package com.hqd.jjvm.classformat.attributeinfo;

import com.hqd.jjvm.classformat.ClassFile;
import com.hqd.jjvm.util.HexStrTransformUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Getter
@NoArgsConstructor
public class ParameterAnnotationAttributeInfo extends AnnotationAttributeInfo {
    public ParameterAnnotationAttributeInfo(Integer attributeNameIndex, Integer attributeLength) {
        super(attributeNameIndex, attributeLength);
    }

    @Getter
    static class ParameterAnnotations {
        private Integer numAnnotations;
        private List<ElementValuePair.Annotation> annotationList;

        public ParameterAnnotations(Integer numAnnotations, List<ElementValuePair.Annotation> annotationList) {
            this.numAnnotations = numAnnotations;
            this.annotationList = annotationList;
        }
    }

    protected List<ParameterAnnotations> parseParameterAnnotations(ClassFile classFile, Integer numParameters) {
        List<ParameterAnnotations> parameterAnnotationList = new ArrayList<>(numParameters);
        this.setByteCodes(classFile.read(this.getAttributeLength() - 1, true).getBytes());
        for (int i = 0; i < numParameters; i++) {
            Integer numAnnotations = HexStrTransformUtil.parseHexToInt(classFile.readU2());
            parameterAnnotationList.add(new ParameterAnnotations(numAnnotations, parseAnnotation(classFile, numAnnotations)));
        }
        return parameterAnnotationList;
    }
}
